<?php
/*  
今日发货情况
*/
declare(strict_types=1);

namespace Modules\Pmcsignage\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Base\Repository\MaterialRepository;
use Modules\Production\Models\ProductionOrder;
use Modules\Production\Models\ProductionOrderDetail;
use Modules\Production\Repository\ProductionOrderRepository;
use Modules\Production\Repository\ProductionOrderStockUserRepository;
use Modules\Production\Service\ProductionOrderService;
use Modules\Pmcsignage\Http\Repositories\TodayproductionReget;
use Modules\Production\Models\ProductionReportOrder;
use Modules\Salesmanagement\Models\Salesout;
use Modules\Salesmanagement\Models\SalesoutAttached;
use Modules\Pmcsignage\Http\Repositories\OrderstodayReget;

class OrderstodayController extends Controller
{
    public function __construct(
        protected readonly ProductionOrder $model
    ){}
   
   
    /**
     * 数据表查询
     *@$fields  string
     *@$where  []
     *@$limit  string
     */
    public static $Plantype = [
        [
            'label'=>'暂存',
            'value'=>'Z',
        ],
        [
            'label'=>'创建',
            'value'=>'A',
        ],
        [
            'label'=>'审核中',
            'value'=>'B',
        ],
        [
            'label'=>'已审核',
            'value'=>'C',
        ],
         [
            'label'=>'重新审核',
            'value'=>'D',
        ],
    ];
 


    /**今日接单情况
     * @param Request $request
     * @return mixed
     */
    public function orderstodayreceived(Request $request): mixed
    {
        $data = $request->all();
        $dataList = OrderstodayReget::tablelist($data);
        foreach (self::$Plantype as $k=>$v){
            $ty[$v['value']] = $v['label'];
        }
     
        $i = 1;
        $OrderTaking = 0;//接单量
        $OrderSku = 0;//接单sku数量
        $OrderTaking_reviewed = 0;//待审单量
        $OrderSku_reviewed = 0;//sku数量
        foreach($dataList as $k=>$v){
            if($v['FDocumentStatus'] == 'B'){
                $OrderTaking_reviewed++;
                $OrderSku_reviewed +=$v['FQty'];
            }
            if($v['FDocumentStatus'] == 'C'){
                $OrderTaking++;
                $OrderSku +=$v['FQty'];
            }
            
            $dataList[$k]['FDocumentStatus'] = isset($ty[$v['FDocumentStatus']])?$ty[$v['FDocumentStatus']]:'';
            $dataList[$k]['fseq'] = $i;
            $i++;
        }
        
        return [
            'head'=>[
                'FDate'=>$data['FDate'],
                'OrderTaking'=>$OrderTaking,
                'OrderSku'=>$OrderSku,
                'OrderTaking_reviewed'=>$OrderTaking_reviewed,
                'OrderSku_reviewed'=>$OrderSku_reviewed,
                ],
            'data'=>$this->page_array($dataList,$data['page'],(int)$data['limit'])
        ];
    }
         // $arr为原始数组，$page为当前页码，$page_size为每页显示数据条目数
    public function page_array($arr, $page=1, $page_size=50000) {
       
        $total = count($arr); // 计算数组总数
        $total_page = ceil($total / $page_size); // 计算总页数
        $offset = ($page - 1) * $page_size;     // 计算当前数据起始位置
        $page_arr = array_slice($arr, $offset, $page_size); // 从原数组中截取$page_size个数据作为当前页数据
        return array(
            'data' => $page_arr,   // 分页数据
            'total' => $total,   // 总记录数
            'page' => $page,     // 当前页码
            'limit' => $page_size   // 每页数据条目数
        );
    }

    /**今日发货情况
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
        $data = OrderstodayReget::listtable($request->all());
        foreach($data as $k=>$v){
            $SalesoutAttached = Salesout::query()
                ->select('fbillno','state','writestate')
                ->where('id',$v['salesout_id'])
                ->first();
            $data[$k]['fbillno'] = $SalesoutAttached->fbillno;
            if ($SalesoutAttached->writestate == 1) {
                $data[$k]['writestate'] = '已出库';
            }else{
                $data[$k]['writestate'] = '未出库';
            }
        }

        return [
            'head'=>OrderstodayReget::tablecont($request->all()),
            'data'=>$data
        ];
        
    }

}
